<script>
	export default {
		onLaunch: async function(e) {
			const that = this

			console.log('小程序App Launch', e)


			// #ifdef MP-WEIXIN
			that.$store.commit('setScene', e.scene)

			this.$http.index.getApp()
				.then(res => {
					console.log(res)
					this.$store.commit('setAppInfo', res.data)
				})
				
			uni.login({ //微信小程序登录取code
				provider: 'weixin',
				success: function(loginRes) {
					that.$http.user.wxlogin({
							code: loginRes.code
						}) //微信小程序登录取token
						.then(res => {
							uni.setStorageSync('token', res.data.token)
							uni.$emit('getuserinfo') //发送获取用户信息的全局请求
						})
						.catch(err => {
							console.log('请求登录接口出错', err)
						})
				},
				fail: function(err) {
					console.log(err);
				}
			});
			
			// #endif

			// #ifdef H5
			that.$store.commit('setScene', -1)

			this.$http.index.getApp()
				.then(AppInfo => {
					console.log('AppInfo', AppInfo)
					that.$store.commit('setAppInfo', AppInfo.data)
					uni.setStorageSync("office_appid", AppInfo.data.official_account_appid); //在localStorage中存储office_appid
					uni.setStorageSync("is_pay", AppInfo.data.is_pay);
				})

			let office_appid = uni.getStorageSync("office_appid");

			if (office_appid) {
				that.authorization(office_appid)
			} else {
				this.$http.index.getApp()
					.then(AppInfo => {
						console.log('AppInfo', AppInfo)
						that.$store.commit('setAppInfo', AppInfo.data)
						uni.setStorageSync("is_pay", AppInfo.data.is_pay);
						uni.setStorageSync("office_appid", AppInfo.data.official_account_appid); //在localStorage中存储office_appid
						office_appid = AppInfo.data.official_account_appid;
						that.authorization(office_appid)
					})
			}
			// #endif

			//监听getuserinfo事件执行获取用户信息并提交
			uni.$on('getuserinfo', () => {
				that.$http.user.getUserInfo()
					.then(res => { //获取用户信息
						that.$store.commit('setUserInfo', res.data)
						uni.$emit('binduser')
						// #ifdef MP-WEIXIN
						uni.$emit('login', res.data.mobile)
						// #endif
						uni.hideLoading()
					})
					.catch(err => {
						uni.hideLoading()
					})
			})
		},
		methods: {
			// #ifdef H5
			getUrlKey: function(name) { // 获取url参数
				//获取url 参数
				return (decodeURIComponent((new RegExp("[?|&]" + name + "=" + "([^&;]+?)(&|#|;|$)").exec(location.href) || [,
					""
				])[1].replace(/\+/g, "%20")) || null);
			},
			authorization(office_appid) { //传入公众号appid，走授权
				/**
				 * 上线用此注释下代码
				 * @param {Object} 直接使用token，进行调试
				 * start
				 */
				var code = this.$route.query.code || this.getUrlKey("code");

				// if (!code) { //授权获取openid
				// 	let urlNow = encodeURIComponent(window.location.href);
				// 	let scope = "snsapi_base"; //  snsapi_base snsapi_userinfo   //静默授权 用户无感知
				// 	let appid = office_appid;
				// 	let state = "123";
				// 	let url =`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${urlNow}&response_type=code&scope=${scope}&state=${state}#wechat_redirect`;

				// 	window.location.href = url;
				// } else {					
				// 	this.$http.index.H5Login({
				// 			code: code
				// 		}) //微信小程序登录取token
				// 		.then(res => {
				// 			uni.setStorageSync('token', res.data.token)
				// 			uni.$emit('getuserinfo') //发送获取用户信息的全局请求
				// 		})
				// 		.catch(err => {
				// 			console.log('请求登录接口出错', err)
				// 		})
				// 		this.$wechat.share()
				// }
				/**
				 * end
				 */


				/**
				 * @param {Object} 直接使用token，进行调试
				 * start
				 */
				let token = '035fc6cb32287ddc38878ffeb8b19ca4';
				uni.setStorageSync('token', token)
				uni.$emit('getuserinfo') //发送获取用户信息的全局请求
				//end

			},
			// #endif

		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		}
	}
</script>

<style lang="scss">
	@import "./static/iconfont.css";
	@import "@/uni_modules/uview-ui/index.scss";

	* {
		box-sizing: border-box;
	}

	.flex {
		display: flex;
	}

	.align-center {
		align-items: center;
	}

	.self-center {
		align-self: flex-center;
	}

	.justify-start {
		justify-content: flex-start;
	}

	.justify-end {
		justify-content: flex-end;
	}

	.justify-center {
		justify-content: center;
	}

	.justify-between {
		justify-content: space-between;
	}

	.justify-around {
		justify-content: space-around;
	}

	.ellipsis {
		/*文字溢出隐藏*/
		overflow: hidden;
		white-space: nowrap;
		text-overflow: ellipsis;
	}
	
</style>
